package com.fifth.business.service.impl;

import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.fifth.business.feign.BusinessFeign;
import com.fifth.business.feign.SsoFeign;
import com.fifth.business.mapper.BusinessMapper;
import com.fifth.business.service.BusinessService;
import com.fifth.business.untils.WebUtils;
import com.fifth.common.pojo.dto.CommonResult;
import com.fifth.common.pojo.entity.BusinessInfo;
import com.fifth.common.pojo.entity.UserInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.DigestUtils;


import javax.servlet.http.HttpServletRequest;
import java.text.SimpleDateFormat;
import java.time.LocalDateTime;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @作者：刘壬杉
 * @创建时间 2022/8/31 11:36
 **/
@Service
public class BusinessServiceImpl implements BusinessService {
    @Autowired
    private BusinessMapper businessMapper;
    @Autowired
    private StringRedisTemplate redisTemplate;
    @Autowired
    private SsoFeign ssoFeign;
    @Autowired
    private BusinessFeign businessFeign;




    /**
     * 根据tel查询商户信息
     * @param telephone
     * @return
     */
    @Override
    public BusinessInfo findByPhone(String telephone) {
        QueryWrapper<BusinessInfo> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("telephone",telephone);
        return businessMapper.selectOne(queryWrapper);
    }



    /*//TODO:不要分页
    @Override
    public CommonResult selectAll(Integer currentPage, Integer pageSize, BusinessInfo businessInfo) {
        PageHelper.startPage(currentPage,pageSize);
        List<BusinessInfo> businessInfos = businessMapper.selectAll(businessInfo);
        PageInfo pageInfo = new PageInfo(businessInfos);
        return new CommonResult(2000,"查询成功",pageInfo);
    }*/

    /**
     * 修改店营业状态
     * @param businessInfo
     * @return
     */
    @Override
    public CommonResult updateById(BusinessInfo businessInfo) {
        int i = businessMapper.updatestore(businessInfo);
        return new CommonResult(2000,"修改成功",i);
    }

    /**
     * 根据id删除
     * @param id
     * @return
     */
    @Override
    public CommonResult storedelete(String id) {
        int i = businessMapper.deleteById(id);
        return new CommonResult(2000,"删除成功",i);
    }

    /**
     * 添加商户
     * @param businessInfo
     * @return
     */
    @Override
    public CommonResult storeinsert(BusinessInfo businessInfo) {
        HttpServletRequest request = WebUtils.getRequest();
        //获取token请求头
        String token = request.getHeader("token");
        //获取登陆者信息
        Map map=JSON.parseObject(redisTemplate.opsForValue().get(token),Map.class);
        Map user = (Map) map.get("user");
        businessInfo.setPassword((String) user.get("password"));
        businessInfo.setSalt((String) user.get("salt"));

        Date date = new Date();
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        //获取当前时间String类型
        String time_string = sdf.format(date);
        businessInfo.setEnterdate(time_string);
        businessInfo.setApplicationdate(time_string);
        /*businessInfo.setPassword(DigestUtils.md5DigestAsHex("123456".getBytes()));*/
        int insert = businessMapper.insert(businessInfo);
        return new CommonResult(2000,"添加成功",insert);
    }

    /**
     * 修改
     * @param businessInfo
     * @return
     */
    @Override
    public CommonResult storeupdate(BusinessInfo businessInfo) {
        int i = businessMapper.updateById(businessInfo);
        return new CommonResult(2000,"修改成功",i);
    }

    /**
     * 修改状态
     * @param businessInfo
     * @return
     */
    @Override
    public CommonResult updatestatus(BusinessInfo businessInfo) {
        QueryWrapper<BusinessInfo> wrapper = new QueryWrapper<>();
        wrapper.eq("sname",businessInfo.getSname());
        BusinessInfo businessInfo1=new BusinessInfo();
        businessInfo1.setState(businessInfo.getState());
        int update = businessMapper.update(businessInfo1, wrapper);
        return new CommonResult(2000,"修改状态成功",update);


    }

    /**
     * 查询商铺信息
     * @return
     */
    @Override
    public CommonResult queryshop() {
       //List<BusinessInfo> businessInfos = businessMapper.selectAll(businessInfo);
       List<BusinessInfo>  queryshop= businessMapper.queryshop();
        return new CommonResult(2000,"修改状态成功", queryshop);
    }

    /**
     * 根据销量前四查询
     * @param
     * @return
     */
    @Override
    public List<BusinessInfo> fourshop() {
        List<BusinessInfo>  fourshop= businessMapper.fourshop();
        return fourshop;
    }

    /**
     * 根据销量查询全部
     * @param
     * @return
     */
    @Override
    public List<BusinessInfo> allshop() {
        List<BusinessInfo> allshop= businessMapper.allshop();
        return allshop;
    }

    /**
     * 查询所有店铺
     * @return
     */
    @Override
    public CommonResult selectshop() {
        List<BusinessInfo>  selectshop= businessMapper.selectshop();
        return new CommonResult(2000,"查询成功",selectshop);
    }

    /**
     * 入驻审核查询
     * @return
     */
    @Override
    public CommonResult loadlingshop(BusinessInfo businessInfo, Integer currentPage, Integer pageSize) {
        IPage<BusinessInfo> page= new Page<>(currentPage,pageSize);
        QueryWrapper<BusinessInfo> wrapper = new QueryWrapper<>();
        wrapper.eq("bb.status",businessInfo.getStatus());
        if (businessInfo.getStatus()==null){
            wrapper = null;
        }
        IPage<BusinessInfo> businessInfoIPage=businessMapper.loadlingshop(page, wrapper);
        System.out.println("总页码:" + businessInfoIPage.getPages());
        System.out.println("总条数:" + businessInfoIPage.getTotal());
        System.out.println("当前页记录:" + businessInfoIPage.getRecords());
        Map<String,Object> map = new HashMap<>();
        map.put("totel",businessInfoIPage.getTotal());
        map.put("pages",businessInfoIPage.getPages());
        map.put("records",businessInfoIPage.getRecords());
        return new CommonResult(2000,"查询成功",map);
    }

    /**
     * 修改用户申请审核状态
     * @param businessInfo
     * @return
     */
    @Override
    @Transactional
    public CommonResult loadingstatus(BusinessInfo businessInfo) {
        System.out.println(businessInfo);
        int i = businessMapper.updateById(businessInfo);
        System.out.println(i);
        String id = businessInfo.getId();
        int sortid = Integer.parseInt(businessInfo.getSortid());
        System.out.println(sortid);
        CommonResult commonResult = businessFeign.insertBusiness(sortid,id );
        System.out.println(commonResult);

        return new CommonResult(2000,"修改成功",i);
    }

    /**
     * 查询店铺数量总数
     * @return
     */
    @Override
    public Map numbershop() {
        Map numshop = businessMapper.numshop();
        return numshop;
    }

    /**
     * 查询店铺ip和店铺名
     * @return
     */
    @Override
    public List<BusinessInfo> ipshop() {
        List<BusinessInfo> ipshop=businessMapper.ipshop();
        return  ipshop;
    }

    /**
     * 根据id修改营业状态
     * @param id
     * @return
     */
    @Override
    public int updatestate(String id) {
        BusinessInfo businessInfo = new BusinessInfo();
        businessInfo.setState(2);
        businessInfo.setId(id);
        int i = businessMapper.updateById(businessInfo);
        return i;
    }

    /**
     * 获取登录商家信息
     * @return
     */
    @Override
    public CommonResult showshop() {
        //获取token
        HttpServletRequest request = WebUtils.getRequest();
        //获取token请求头
        String token = request.getHeader("token");
        //获取登陆者信息
        Map map= JSON.parseObject(redisTemplate.opsForValue().get(token),Map.class);
        if (map!=null && map.containsKey("user")) {
            Map user = (Map) map.get("user");
            String id = (String) user.get("id");
            BusinessInfo businessInfo =businessMapper.selectById(id);
            return new CommonResult(2000, "查询成功", businessInfo);

        }
        return new CommonResult(4000,"用户未登录",null);
    }

    /**
     * 修改登录商家信息
     * @param businessInfo
     * @return
     */
    @Override
    public CommonResult changeshop(BusinessInfo businessInfo) {
        HttpServletRequest request = WebUtils.getRequest();
        //获取token请求头
        String token = request.getHeader("token");
        //获取登陆者信息
        Map map=JSON.parseObject(redisTemplate.opsForValue().get(token),Map.class);
        Map user = (Map) map.get("user");
        businessInfo.setId((String) user.get("id"));
        int i = businessMapper.updateById(businessInfo);
        return new CommonResult(2000, "修改成功", null);
    }

    /**
     * 修改商家密码
     * @param telephone
     * @param password
     * @return
     */
    @Override
    public CommonResult changepass(String telephone, String password) {
        HttpServletRequest request = WebUtils.getRequest();
        //获取token请求头
        String token = request.getHeader("token");
        //获取登陆者信息
        Map map=JSON.parseObject(redisTemplate.opsForValue().get(token),Map.class);
        //拿到redis里面的缓存内容
        Map user = (Map) map.get("user");
        String salt = (String) user.get("salt");
        String id = (String) user.get("id");
        String tele = (String) user.get("telephone");
        //判断用户手机号是否与登录信息相同
        if (telephone.equals(tele)){
            //openfeign远程调用加密
            //远程调用加密存入数据库
            String s = ssoFeign.signInsert(password, salt);
            BusinessInfo businessInfo = new BusinessInfo();
            businessInfo.setId(id);
            businessInfo.setPassword(s);
            int i = businessMapper.updateById(businessInfo);
            return new CommonResult(2000,"修改成功,请重新登录",null);
        }
        return new CommonResult(5000,"该手机号错误",null);
    }

    @Override
    public  List<Map> numberBusiness() {
        return businessMapper.numberBusiness();
    }


    @Override
    public List<Map> priceBusiness() {
        return businessMapper.priceBusiness();
    }

    /**
     * 查询全部
     * @param bid
     * @return
     */
    @Override
    public BusinessInfo selectmarchant(String bid) {
        return businessMapper.selectById(bid);
    }

    /**
     * 根据token判断当前用户
     * @return
     */
    @Override
    public CommonResult tokenphone() {
        HttpServletRequest request = WebUtils.getRequest();
        //获取token请求头
        String token = request.getHeader("token");
        //获取登陆者信息
        Map map=JSON.parseObject(redisTemplate.opsForValue().get(token),Map.class);
        //拿到redis里面的缓存内容
        Map user = (Map) map.get("user");

        QueryWrapper<BusinessInfo> wrapper = new QueryWrapper<>();
        wrapper.eq("telephone",user.get("telephone"));

        BusinessInfo businessInfo1 = businessMapper.selectOne(wrapper);
        System.out.println(businessInfo1);
        if (businessInfo1!=null) {
            return new CommonResult(2000,"查询成功",true);
        }
        return new CommonResult(2000,"查询失败",false);
    }


}
